<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>楚瑞涛的博客管理后台-追评管理</title>
  <link rel="stylesheet" href=<?php echo HOME_CSS.'layui.css' ?> media="all">
  <script type="text/javascript" src=<?php echo HOME_JS.'jquery.min.js' ?> ></script>
  <script src=<?php echo HOME_JS.'layui.js' ?> ></script>
  <style>
    .info-title{
      text-align: center;
      font-weight: bold;
    }
    .info-item{
      padding: 5px 10px;
    }
    .info-item>input,.info-item>select{
      width:100px;
    }
    .layui-input-block>span{
      line-height: 34px;
    }
    </style>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo">楚瑞涛博客管理后台</div>
    <!-- 头部区域（可配合layui已有的水平导航） -->
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item">
        <a href="javascript:;">
          <img src=<?php echo $_SESSION['m_portrait'] ?>  class="layui-nav-img" alt="头像">
          楚瑞涛
        </a>
        <dl class="layui-nav-child">
          <dd><a href=<?php echo HOME_PATH.'index.php/User/getManAll' ?>>基本资料</a></dd>
          <dd><a href=<?php echo HOME_PATH.'index.php/User/getManagerPass' ?>>修改密码</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item"><a onclick="exit_login()" href="javascript:;">退出</a></li>
    </ul>
  </div>

  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-nav-tree"  lay-filter="test">
        <li class="layui-nav-item">
          <a class="" href="javascript:;">用户管理
            <span class="layui-nav-more"></span>
          </a>
          <dl class="layui-nav-child">
            <dd><a href=<?php echo HOME_PATH.'index.php/User/getUserAll' ?>>用户管理</a></dd>
            <!-- <dd><a href="javascript:;">权限申请管理</a></dd> -->
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">博客管理
            <span class="layui-nav-more"></span>
          </a>
          <dl class="layui-nav-child">
            <dd><a href=<?php echo HOME_PATH.'index.php/Blog/getBlogAll' ?>>博客管理</a></dd>
            <dd><a href=<?php echo HOME_PATH.'index.php/Type/getTypeAll' ?>>类别管理</a></dd>
            <dd><a href=<?php echo HOME_PATH.'index.php/Target/getTargetAll' ?>>标签管理</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item layui-nav-itemed">
            <a href="javascript:;">评论管理</a>
            <dl class="layui-nav-child">
              <dd><a href=<?php echo HOME_PATH.'index.php/Comment/getCommentAll' ?> >评论管理</a></dd>
              <dd class="layui-this"><a href=<?php echo HOME_PATH.'index.php/Reply/getReplyAll' ?> >追评管理</a></dd>
            </dl>
        </li>
        <li class="layui-nav-item">
            <a href="javascript:;">系统设置</a>
            <dl class="layui-nav-child">
              <dd><a href=<?php echo HOME_PATH.'index.php/User/getManAll' ?> >个人信息</a></dd>
              <dd><a href=<?php echo HOME_PATH.'index.php/User/getManagerPass' ?> >修改密码</a></dd>
            </dl>
        </li>
      </ul>
    </div>
  </div>
  
  <div class="layui-body">
    <!-- 内容主体区域 -->
    <div style="padding: 15px;">
      <form class="layui-form" action="">
        <div class="layui-form-item">搜索内容：
          <div class="layui-inline">
          <input type="text" id="content" name="content" required lay-verify="required" placeholder="请输入追评内容" autocomplete="off" class="layui-input">
        </div>
        <button class="layui-btn layui-btn-normal" onclick="return false;" data-type="reload" id="search" >搜索</button>
        <button class="layui-btn layui-btn-primary " onclick="return false;" id="refresh" data-type="reload" >刷新</button>
        </div>
      </form>
      <table class="layui-hide" id="reply_info" lay-filter="reply_info"></table>
      <script type="text/html" id="replyBar">
          <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
          <a class="layui-btn layui-btn-xs" lay-event="set">设置</a>
          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="hide">不可见</a>
      </script>
      <div class="layui-form layui-form-pane" style="display:none" id="setReply">
        <h3 class="info-title">设置追评</h3>
        <form class="layui-form">
          <div class="layui-form-item">
            <label class="layui-form-label">博客标题：</label>
            <div class="layui-input-block">
              <span name="title" ></span>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">追评昵称：</label>
            <div class="layui-input-block">
              <input type="text" name="replay_name" required  lay-verify="required" placeholder="请输入用户昵称" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">评论内容：</label>
            <div class="layui-input-block">
              <textarea name="reply_content" placeholder="请输入评论内容" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">点赞数量：</label>
            <div class="layui-input-block">
            <input type="text" name="praise" required  lay-verify="required" placeholder="请输入点赞数量" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">评论状态：</label>
            <div class="layui-input-block">
              <input type="checkbox" name="state" lay-skin="switch" lay-text="可见|不可见">
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
  
<script>
 //退出登陆
 function exit_login(){
    layer.alert('确定要退出嘛？',{
      // skin: 'layui-layer-rim' //样式类名 /layui-layer-molv  layui-layer-lan   layui-layer-rim
      closeBtn: 1    // 是否显示关闭按钮
      ,anim: 1 //动画类型
      ,btn: ['退出','取消'] //按钮
      ,icon: 3    // icon\
      ,title:"退出"
      ,yes:function(){
        $.ajax({
          url:'/blog_system/admin/index.php/Login/exitLogin',
          type:'POST',
          contentType: "application/x-www-form-urlencoded;charset=utf-8",
          success:function(result){
            console.log(result);
            if(result == 1){
              layer.msg('退出登陆成功',{icon: 6, time: 2000});
              $(":root").delay(2000).queue(function(){ window.location.href="/blog_system/admin/index.php"; });   //阿里云搜索跳转
            }else{
              layer.msg('退出登陆失败，请刷新后重试',{icon:5, time: 2000});
            }
          }
        });
      }
    });
  }
  //使用后台布局组件
  layui.use(['element','table',"layer",'form'], function(){
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer,$=layui.$;
    var element = layui.element;
    
    table.render({
      elem: '#reply_info'
      ,url:'/blog_system/admin/index.php/Reply/getReplysJson'
      ,title: '博客数据表'
      ,toolbar: 'default'      //以后再写
      ,cellMinWidth: 120 //全局定义常规单元格的最小宽度
      ,cols: [[
          {type: 'checkbox', fixed: 'left'}
          ,{field:'id', title:'编号', width:80, unresize: true, type:'numbers'}
          ,{field:'title', title:'博客标题', align: 'center', width:150}
          ,{field:'comment_name', title:'评论昵称', align: 'center', width:150}
          ,{field:'comment_content', title:'评论内容', align: 'center' }
          ,{field:'reply_name', title:'追评昵称', align: 'center', width:150}
          ,{field:'reply_content', title:'追评内容', align: 'center' }
          ,{field:'time', title:'追评时间', align: 'center', sort: true, width:150 }
          ,{field:'praise_count', title:'点赞数量', align: 'center', width:100 }
          ,{field:'state', title:'状态', align: 'center', sort: true, width:100}
          ,{fixed: 'right', title:'操作', toolbar: '#replyBar', align: 'center', width:180}
      ]]
      ,height: 'full-220'   //高度自适应
      ,limit:15
      ,page: {
        layout: [ 'prev', 'page', 'next', 'count','skip'] //自定义分页布局
        ,curr: 1 
        ,first: '首页' //显示首页
      }
    });
    $("#refresh").on('click',function(){
      table.reload('reply_info');
    });
    //点击搜索按钮根据用户名称查询
	  $('#search').on('click',function(){
      if($('#content') == ''){
        return;
      }
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
    //根据条件查询表格数据重新加载
	  var $ = layui.$, active = {
		    reload: function(){
		    //获取用户名
		      var content = $('#content');
		      //执行重载
		      table.reload('reply_info', {
		        page: {
		          layout: [ 'prev', 'page', 'next', 'count','skip'] //自定义分页布局
              ,curr: 1 
              ,first: '首页' //显示首页
		        }
            ,where: {       //根据条件查询
		        	content:content.val()
		        }
		      });
		   }
		};
    //监听头工具栏事件
    table.on('toolbar(reply_info)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id)
      ,data = checkStatus.data; //获取选中的数据
      switch(obj.event){
        case 'add':
        if(data.length === 0){
          alert('暂未开启');
        }
        break;
        case 'update':
          if(data.length === 0){
            layer.msg('请选择一行',{icon: 7, time: 2000});
          } else if(data.length > 1){
            layer.msg('只能同时编辑一个',{icon: 7, time: 2000});
          } else {
            layer.open({
              title: '设置'  
              ,btn: ['修改','取消'] //按钮
              ,anim: 1 //动画类型 
              ,content:$('#setReply').html()
              ,success: function(layero, index){
                console.log(layero);
                layero.find('span[name="title"]').html(data[0].title);
                layero.find('input[name="replay_name"]').val(data[0].reply_name);
                layero.find('textarea[name="reply_content"]').val(data[0].reply_content);
                layero.find('input[name="praise"]').val(data[0].praise_count);
                if(data[0].state=='可见')
                  layero.find('input[name="state"]').prop("checked",true);
                form.render();    //重新渲染 from 组件
              }
              ,yes:function(index,layero){
                var replay_name = layero.find('input[name="replay_name"]').val().trim();
                var content = layero.find('textarea[name="reply_content"]').val().trim();
                var praise_count = layero.find('input[name="praise"]').val();
                var state = layero.find('input[name="state"]').prop("checked")?1:0;
                var re = /^[0-9]*$/;
                var uPattern = /^[a-zA-Z0-9_-]{2,16}$/;//用户名正则，4到16位（字母，数字，下划线，减号）
                if(!uPattern.test(replay_name)){
                  alert('用户名输入2-16位，且不能包含特殊字符');
                  return;
                }else if(!re.test(praise_count)){
                  alert(praise_count);
                  alert('请输入正确的点赞数量');
                  return;
                }
                datainfo={
                  user_name:replay_name,
                  content:content,
                  praise_count:praise_count,
                  state:state,
                  id:data[0].id
                }
                $.ajax({
                  url:'/blog_system/admin/index.php/Reply/editReply',
                  type:'POST',
                  data:datainfo,
                  contentType: "application/x-www-form-urlencoded;charset=utf-8",
                  success:function(result){
                    if(result){
                      var data = JSON.parse(result);
                      if(data.code==1){
                        layer.msg('修改成功',{icon: 6, time: 2000});
                        $(".layui-laypage-btn")[0].click();       //刷新当前页面数据
                      }else{
                        layer.msg('修改失败，数据没有变化',{icon: 7, time: 2000});
                      }
                      return ; 
                    }
                    layer.msg('修改失败',{icon: 5, time: 2000}); 
                  }
                });
              }
            });
          }
        break;
        case 'delete':
          if(data.length === 0){
            layer.msg('请选择一行',{icon: 7, time: 2000});
          }else if(data.length > 1 ){
            layer.msg('一次最多删除1个',{icon: 7, time: 2000});
          } else {
            layer.alert('真的删除选中的数据吗？<br>删除后数据将不可恢复！！',{
              closeBtn: 1    // 是否显示关闭按钮
              ,anim: 6 //动画类型
              ,btn: ['确定','取消'] //按钮
              ,icon: 3    // icon
              ,title:"删除"
              ,yes:function(index){
                var data = checkStatus.data;
                dataInfo = {
                  id : data[0].id
                } 
                $.ajax({
                  url:'/blog_system/admin/index.php/Reply/delReply',
                  type:'POST',
                  contentType: "application/x-www-form-urlencoded;charset=utf-8",
                  data:dataInfo,
                  success:function(result){
                    if(result){
                      var data = JSON.parse(result);
                      console.log(data.code);
                      if(data.code == 1){
                        layer.msg('删除成功',{icon: 6, time: 2000});
                        $(".layui-laypage-btn")[0].click();       //刷新当前页面数据
                      }else{
                        layer.msg('删除失败',{icon: 7, time: 2000});
                      }
                      return ; 
                    }
                    layer.msg('删除失败',{icon: 5, time: 2000}); 
                  }
                });
              }
            });
          }
        break;
      }
    });
    //监听工具条
    table.on('tool(reply_info)', function(obj){
      var replyInfo = obj.data;

      console.log(replyInfo);
      if(obj.event === 'detail'){
        var replyInfo = obj.data;
        console.log(replyInfo);
        layer.open({
          title: '查看'
          ,anim: 1 //动画类型     
          ,content:
          '<div>'+
            '<h3 class="info-title">追评信息</h3>'+
            '<p class="info-item"><span>博客标题：</span>'+replyInfo.title+'</p>'+
            '<p class="info-item"><span>评论昵称：</span>'+replyInfo.comment_name+'</p>'+
            '<p class="info-item"><span>评论内容：</span>'+replyInfo.comment_content+'</p>'+
            '<p class="info-item"><span>追评昵称：</span>'+replyInfo.reply_name+'</p>'+
            '<p class="info-item"><span>追评内容：</span>'+replyInfo.reply_content+'</p>'+
            '<p class="info-item"><span>追评邮箱：</span>'+replyInfo.user_mail+'</p>'+
            '<p class="info-item"><span>评论时间：</span>'+replyInfo.time+'</p>'+
            '<p class="info-item"><span>状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态：</span>'+replyInfo.state+'</p>'+
          '</div>'
        });
      } else if(obj.event === 'set'){
        layer.open({
          title: '设置'  
          ,btn: ['修改','取消'] //按钮
          ,anim: 1 //动画类型 
          ,content:$('#setReply').html()
          ,success: function(layero, index){
            console.log(layero);
            layero.find('span[name="title"]').html(replyInfo.title);
            layero.find('input[name="replay_name"]').val(replyInfo.reply_name);
            layero.find('textarea[name="reply_content"]').val(replyInfo.reply_content);
            layero.find('input[name="praise"]').val(replyInfo.praise_count);
            if(replyInfo.state=='可见')
              layero.find('input[name="state"]').prop("checked",true);
              form.render();    //重新渲染 from 组件
          }
          ,yes:function(index,layero){
            var replay_name = layero.find('input[name="replay_name"]').val().trim();
            var content = layero.find('textarea[name="reply_content"]').val().trim();
            var praise_count = layero.find('input[name="praise"]').val();
            var state = layero.find('input[name="state"]').prop("checked")?1:0;
            var uPattern = /^[a-zA-Z0-9_-]{2,16}$/;//用户名正则，4到16位（字母，数字，下划线，减号）
            var re = /^[0-9]*$/;
            if(!uPattern.test(replay_name)){
              alert('用户名输入2-16位，且不能包含特殊字符');
              return;
            }else if(!re.test(praise_count)){
              alert('请输入正确的点赞数量');
              return;
            }

            datainfo={
              user_name:replay_name,
              content:content,
              praise_count:praise_count,
              state:state,
              id:replyInfo.id
            }
            $.ajax({
              url:'/blog_system/admin/index.php/Reply/editReply',
              type:'POST',
              data:datainfo,
              contentType: "application/x-www-form-urlencoded;charset=utf-8",
              success:function(result){
                if(result){
                  var data = JSON.parse(result);
                  if(data.code==1){
                    layer.msg('修改成功',{icon: 6, time: 2000});
                    obj.update({      //更新表格数据
                      'reply_name':replay_name,
                      'reply_content':content,
                      'praise_count':praise_count,
                      'state':state==1?'可见':'不可见',
                    });
                  }else{
                    layer.msg('修改失败，数据没有变化',{icon: 7, time: 2000});
                  }
                  return ; 
                }
                layer.msg('修改失败',{icon: 5, time: 2000}); 
              }
            });
          }
        });
      } else if(obj.event === 'hide'){         //设置私密按钮
        layer.alert('确定要隐藏这条追评吗？',{
          // skin: 'layui-layer-rim' //样式类名 /layui-layer-molv  layui-layer-lan   layui-layer-rim
          closeBtn: 1    // 是否显示关闭按钮
          ,anim: 1 //动画类型
          ,btn: ['确定','取消'] //按钮
          ,icon: 3    // icon\
          ,title:"设为不可见"
          ,yes:function(index){
            if(replyInfo.state == '不可见'){
              layer.msg('该追评已经为不可见了',{icon: 7, time: 2000});
              return;
            }
            $.ajax({
              url:'/blog_system/admin/index.php/Reply/setReplyHide/id/'+replyInfo.id,
              type:'POST',
              contentType: "application/x-www-form-urlencoded;charset=utf-8",
              success:function(result){
                if(result){
                  var data = JSON.parse(result);
                  console.log(data.code);
                  if(data.code == 1){
                    layer.msg('设置成功',{icon: 6, time: 2000});
                    obj.update({      //更新表格数据
                      'state':'不可见'
                    });
                  }else{
                    layer.msg('该追评已经为不可见了',{icon: 7, time: 2000});
                  }
                  return ; 
                }
                layer.msg('设置失败',{icon: 5, time: 2000}); 
              }
            });
          }
        });
      }
    });
  });
</script>
  <div class="layui-footer">
    <!-- 底部固定区域 -->
    &copy; 河南科技学院-三月小组
  </div>
</div>  

</body>
</html>